package cn.client.ui.view.main.mindfulness

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import cn.client.R
import cn.client.data.remoteData.bean.DateUtils
import cn.client.data.remoteData.bean.PageSelectByUid
import cn.client.data.userStateManager
import cn.client.navigation.destinations.MindfulnessGroupBuildPage

// 正念团体
@Composable
fun MindfulnessGroup(
    navController: NavController,
    viewModel: MindfulnessViewModel

) {

    val userIdState = userStateManager.userId.collectAsState(initial = -1)
    val userId = userIdState.value

    // 观察状态
    val joinedGroupListState = viewModel.joinedGroupList.collectAsState(initial = emptyList())
    val joinedGroupList = joinedGroupListState.value

    val selectByUid = PageSelectByUid(uid = userId, pageNo = 1, pageSize = 10)

    // 获取已加入的正念团体
    viewModel.dispatch(MindfulnessViewModel.PageIntent.GetJoinedGroupList(selectByUid))

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
    ) {

        CreateGroupButton(
            onClickCreateGroup = {
                // 跳转创建团体页面
                navController.navigate(MindfulnessGroupBuildPage.route)
            }
        )
        LazyColumn(){
            items(joinedGroupList){it ->
                MindfulnessGroupItem(
                    groupName = it.groupName,
                    groupInfo = it.groupInfo
                )
            }
        }


    }
}


// 正念团体item
@Composable
fun MindfulnessGroupItem(
    groupName: String,
    groupInfo: String,
){

    Row {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center,
            modifier = Modifier.size(80.dp)
        ) {
            Image(
                painter = painterResource(R.drawable.ic_peoples),
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier.size(40.dp)
            )
        }
        Column {
            Row(
                horizontalArrangement = Arrangement.Start,
                modifier = Modifier
                    .padding(2.dp)
            ) {
                Text(text = groupName, fontSize = 16.sp)
            }
            Row(
                horizontalArrangement = Arrangement.Start,
                modifier = Modifier
                    .padding(2.dp)
                    .fillMaxWidth()
            ) {
                Text(text = groupInfo, modifier = Modifier.fillMaxWidth(), color = Color.Gray, fontSize = 14.sp)
            }
        }
    }
}


// 创建团体按钮
@Composable
fun CreateGroupButton(
    onClickCreateGroup: () -> Unit
){
    Row (
        horizontalArrangement = Arrangement.End,
        modifier = Modifier.fillMaxWidth()
    ){
        Row (
            horizontalArrangement = Arrangement.Center,
            modifier = Modifier.padding(5.dp)
        ){
            Button(
                onClick = {
                    /* 发送创建团体意图 */
                    onClickCreateGroup()
                }) {
                Text(text = "创建团体")
            }
        }
    }
}